<template>
  <div class="print-wrapper" v-loading="state.loading">
    <template v-for="(item,index) in state.data">
      <component 
      :is="currentCompo" 
      :data="state.data[index]||{}" 
      :date="state.date"
      :op="state.op"
      :bjno="route.query.tzid"/>
    </template>
  </div>
</template>
<script setup lang="ts">
import { computed, onMounted, reactive } from 'vue';
import { useRoute } from 'vue-router';
import createAxios from '/@/utils/axios';
import doc1 from './components/doc1.vue';
import doc2 from './components/doc2.vue';
import doc3 from './components/doc3.vue';
import doc4 from './components/doc4.vue';
import doc5 from './components/doc5.vue';
import doc6 from './components/doc6.vue';
import doc7 from './components/doc7.vue';
import doc8 from './components/doc8.vue';
import doc9 from './components/doc9.vue';
import doc10 from './components/doc10.vue';
import doc11 from './components/doc11.vue';
import doc12 from './components/doc12.vue';
import doc15 from './components/doc15.vue';
import doc17 from './components/doc17.vue';
import doc18 from './components/doc18.vue';
import { ElMessage } from 'element-plus';


const route = useRoute()
const compos:{
  [key:string]:object
} = reactive({
  doc1,doc2,doc3,doc4,doc5,doc6,doc7,doc8,doc9,doc10,doc11,doc12,doc15,doc17,doc18
})
const currentCompo = computed(()=>{
  return compos['doc'+route.query.type]
})


const state:{
  data:any[]
  date:object
  op:object
  loading:boolean
} = reactive({
  data:[],
  date:{},
  op:{},
  loading:true
})

onMounted(()=>{
  getData()
  initDate()
})

const getData = ()=>{
  // if(route.query.type=='13'){
  //   let data = getMock()
  //   state.data = [{
  //     ...data[0],
  //     itemList:data
  //   }]
  //   console.log(state.data)
  //   return
  // }

  state.loading = true
  if(route.query.type=='17'||route.query.type=='18'){
    
  }else{
    createAxios({
      url:'admin/dzd/print_z',
      params:route.query
    }).then((res:any)=>{
      state.loading = false
      if(res.data.length){
        if(route.query.type == '12' || route.query.type == '15' ){
          let obj = res.data[0]
          obj.itemList = res.data
          state.data = [obj]
        }else{
          state.data = res.data
        }
        //获取操作人信息
        if(state.data[0].op){
          getOp(state.data[0].op) 
        }
      }else{
        ElMessage.error('暂无可打印数据！')
        state.data = [{}]
      }
    })
  }
}

const getOp = (id:string)=>{
  createAxios({
    url:'/admin/auth.admin/index?initKey=id&initValue='+id
  }).then((res:any)=>{
    state.op = res.data.list[0] || {}
  })
}

const initDate = ()=>{
  let date = new Date()
  let year = date.getFullYear()
  let month = date.getMonth() +1
  let day = date.getDate()

  state.date =  {
    year,month,day
  }
}


const getMock = ()=>{

  return [
{
      "itemcode": "AB",
      "tzid": "2103SHLBL0001",
      "ybh": "",
      "hbl": "",
      "vesselvoy": " ",
      "portload": "太仓",
      "portdis": "天津",
      "etd": null,
      "nextetd": "16/3/2021 08:44:00",
      "saler": "10016",
      "shipper": "碧辟润滑油（测试）",
      "csnee": "",
      "consum": 2,
      "mntrno": null,
      "cntrno": null,
      "usdrate": 7.043700218200684,
      "itemen": "安保",
      "rmb0": 1.0,
      "usd0": 0.0,
      "rmb1": 0.0,
      "usd1": 0.0,
      "corprmb0": "碧辟润滑油（测试）",
      "corpusd0": "",
      "corprmb1": "",
      "corpusd1": "",
      "taxrate": 0.05999999865889549
    },
    {
      "itemcode": "BAF",
      "tzid": "2103SHLBL0001",
      "ybh": "",
      "hbl": "",
      "vesselvoy": " ",
      "portload": "太仓",
      "portdis": "天津",
      "etd": null,
      "nextetd": "16/3/2021 08:44:00",
      "saler": "10016",
      "shipper": "碧辟润滑油（测试）",
      "csnee": "",
      "consum": 2,
      "mntrno": null,
      "cntrno": null,
      "usdrate": 7.043700218200684,
      "itemen": "BAF",
      "rmb0": 0.0,
      "usd0": 0.0,
      "rmb1": 555.0,
      "usd1": 0.0,
      "corprmb0": "",
      "corpusd0": "",
      "corprmb1": "上海安诺",
      "corpusd1": "",
      "taxrate": 0.05999999865889549
    }
]

}


</script>
<style lang="scss" scoped>

html,body,#app{
  height: auto !important;
  overflow: auto !important;
}
.print-wrapper{
  min-height: 100vh;
}
</style>
